package com.silence.qhyh.webpro.mvp.ui.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.silence.qhyh.webpro.R;
import com.silence.qhyh.webpro.mvp.model.entity.VipImgBean;

import java.util.List;
import java.util.Vector;

/**
 * @author silence
 */
public class VipUpdateAdapter extends BaseQuickAdapter<VipImgBean, BaseViewHolder> {

    private int lastPosition = -1; //记录上一次选中的图片位置，-1表示未选中
    private Vector<Boolean> vector = new Vector<Boolean>();

    public VipUpdateAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
        for (int i = 0; i < data.size(); i++) {
            vector.add(false);
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, VipImgBean item) {
        ImageView imgVip = helper.getView(R.id.iv_vip);
        imgVip.setImageResource(item.getImageResource());
        ImageView selState = helper.getView(R.id.iv_vip_sel_state);
        if (vector.elementAt(helper.getLayoutPosition()) == true) {
            selState.setVisibility(View.VISIBLE);
        } else {
            selState.setVisibility(View.INVISIBLE);
        }
        imgVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastPosition != -1) {
                    vector.setElementAt(false, lastPosition);//取消上一次的选中状态
                }
                vector.setElementAt(!vector.elementAt(helper.getLayoutPosition()), helper.getLayoutPosition()); //直接取反即可
                lastPosition = helper.getLayoutPosition();//记录本次选中的位置
                notifyDataSetChanged();
            }
        });
    }
}
